<% this.title = contest.title + ' - 排名' %>
<% include header %>
<% include contest_header %>
<style>
.submit_time {
  font-size: 0.8em;
  margin-top: 5px;
  color: #000;
}
</style>
<div style="overflow-x:auto; margin-top:14px">
  <table class="ui very basic center aligned table">
    <thead>
    <tr>
        <th>#</th>
        <th>用户名</th>
        <% if (contest.type === 'ACM') { %>
          <th>通过数量</th>
          <th>罚时</th>
        <% } %>
        <% for (let i = 0; i < problems.length; i++) { %>
        <th>
          <a href="<%= syzoj.utils.makeUrl(['contest', contest.id, 'problem', i + 1]) %>">
            <%= syzoj.utils.removeTitleTag(problems[i].title) %>
          </a>
        </th>
        <% } %>
        <% if (contest.type === 'NOI' || contest.type === 'IOI') { %>
          <th>总分</th>
        <% } %>
    </tr>
    </thead>
    <tbody>
    <%
    // for (let problem of problems) {
    //   let i = 0, min, minPos = -1;
    //   for (let item of ranklist) {
    //     i++;
    //     let condition;
    //     if (contest.type === 'ACM') condition = item.player.after_score_details[problem.id] && item.player.after_score_details[problem.id].accepted && (minPos === -1 || item.player.after_score_details[problem.id].acceptedTime < min.player.after_score_details[problem.id].acceptedTime);
    //     else condition = item.player.after_score_details[problem.id] && item.player.after_score_details[problem.id].score === 100 && (minPos === -1 || item.player.after_score_details[problem.id].judge_state.submit_time < min.player.after_score_details[problem.id].judge_state.submit_time);
    //     if (condition) {
    //       min = item;
    //       minPos = i;
    //     }
    //   }
    //   problem.min = minPos;
    // }

    let i = 0, rank = 0, lastItem;
    for (let item of ranklist) {
      i++;
      let latest = contest.start_time, timeSum = 0, unacceptedCount = 0;
    %>
    <tr>
        <%
          if (contest.type === 'NOI' || contest.type === 'IOI') {
            if (i === 1 || item.player.after_score !== lastItem.player.after_score) rank = i;
          } else if (contest.type === 'ACM') {
            for (let problem of problems) {
              if (item.player.after_score_details[problem.id] && item.player.after_score_details[problem.id].accepted) {
                timeSum += (item.player.after_score_details[problem.id].acceptedTime - contest.start_time) + (item.player.after_score_details[problem.id].unacceptedCount * 20 * 60);
                unacceptedCount += item.player.after_score_details[problem.id].unacceptedCount;
              }
            }
            item.player.timeSum = timeSum;

            if (i === 1 || item.player.after_score !== lastItem.player.after_score || item.player.timeSum !== lastItem.player.timeSum) rank = i;
          }
        %>
        <td>
          <% if (rank == 1) { %>
            <div class="ui yellow ribbon label">
          <% } else if (rank == 2) { %>
            <div class="ui ribbon label">
          <% } else if (rank == 3) { %>
            <div class="ui brown ribbon label" style="background-color: #C47222 !important;">
          <% } else { %>
            <div>
          <% } %>
          <%= rank %>
          </div>
        </td>
        <td><a href="<%= syzoj.utils.makeUrl(['user', item.user.id]) %>"><span class="user-name user-<%= syzoj.utils.makeUserColor(item.user.color, item.user.is_admin) %>"><%= item.user.username %></span></a><%- item.user.nickname %><span class="user-nameplate user-gray"><%= item.user.nameplate %></span></td>
        <% if (contest.type === 'ACM') { %>
          <td>
            <span class="score score_<%= parseInt((item.player.after_score / ranklist[0].player.after_score * 10) || 0) %>">
              <%= item.player.after_score %>
            </span>
          </td>
          <td>
            <%= syzoj.utils.formatTime(timeSum) %>
          </td>
        <% } %>
        <% for (let problem of problems) { %>
          <% if (problem.min === i) { %>
            <td style="background: rgb(244, 255, 245); ">
          <% } else { %>
            <td>
          <% } %>
          <% if (!item.player.after_score_details[problem.id]) { %>
            </td>
          <% } else if (contest.type === 'ACM') { %>
              <a href="<%= syzoj.utils.makeUrl(['submission', item.player.after_score_details[problem.id].judge_id]) %>">
                <% if (item.player.after_score_details[problem.id].accepted) { %>
                  <span class="score score_10">
                    <% if (item.player.after_score_details[problem.id].unacceptedCount) { %>
                      +<%= item.player.after_score_details[problem.id].unacceptedCount %>
                    <% } else { %>
                      +
                    <% } %>
                  </span>

                  <div class="submit_time">
                    <%= syzoj.utils.formatTime(item.player.after_score_details[problem.id].acceptedTime - contest.start_time) %>
                  </div>
                <% } else if (item.player.after_score_details[problem.id].unacceptedCount) { %>
                  <span class="score score_0">
                    -<%= item.player.after_score_details[problem.id].unacceptedCount %>
                  </span>
                <% } %>
              </a>
            </td>
          <% } else if (contest.type === 'NOI' || contest.type === 'IOI') { %>
              <a href="<%= syzoj.utils.makeUrl(['submission', item.player.after_score_details[problem.id].judge_id]) %>">
                <% if (item.player.after_score_details[problem.id].weighted_score != null) { %>
                  <span class="score score_<%= parseInt((item.player.after_score_details[problem.id].score / 10) || 0) %>">
                    <%= Math.round(item.player.after_score_details[problem.id].weighted_score) %>
                  </span>
                <% } else { %>
                  <span class="status compile_error">
                    0
                  </span>
                <% } %>
              </a>
              <div class="submit_time">
                <%= syzoj.utils.formatTime(item.player.after_score_details[problem.id].judge_state.submit_time - contest.start_time) %>
                <% latest = Math.max(latest, item.player.after_score_details[problem.id].judge_state.submit_time)  %>
              </div>
            </td>
          <% } %>
        <% } %>
        <% if (contest.type === 'NOI' || contest.type === 'IOI') { %>
          <td>
            <span class="score score_<%= parseInt((item.player.after_score / ranklist[0].player.after_score * 10) || 0) %>">
              <%= item.player.after_score %>
            </span>
            <div class="submit_time">
              <%= syzoj.utils.formatTime(latest - contest.start_time) %>
            </div>
          </td>
        <% } %>
    </tr>
    <% lastItem = item; %>
    <% } %>
    </tbody>
  </table>
</div>
<% if (!ranklist.length) { %>
<div style="background-color: #fff; height: 18px; margin-top: -18px; "></div>
<div class="ui placeholder segment" style="margin-top: 0px; ">
  <div class="ui icon header">
    <i class="ui file icon" style="margin-bottom: 20px; "></i>
    暂无选手提交
  </div>
</div>
<% } %>
<% include contest_footer %>
<% include footer %>
